package cn.tl.hrm.service.impl;

import cn.tl.hrm.client.SearchServiceClient;
import cn.tl.hrm.constants.CourseConstants;
import cn.tl.hrm.doc.CourseDoc;
import cn.tl.hrm.domain.Course;
import cn.tl.hrm.domain.CourseDetail;
import cn.tl.hrm.domain.CourseMarket;
import cn.tl.hrm.dto.CourseAddParamDTO;
import cn.tl.hrm.exception.Assert;
import cn.tl.hrm.exception.BusinessException;
import cn.tl.hrm.mapper.CourseMapper;
import cn.tl.hrm.service.ICourseDetailService;
import cn.tl.hrm.service.ICourseMarketService;
import cn.tl.hrm.service.ICourseService;
import cn.tl.hrm.util.AjaxResult;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kaizi
 * @since 2022-04-11
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {
    @Autowired
    private ICourseMarketService courseMarketService;
    @Autowired
    private ICourseDetailService courseDetailService;
    @Autowired
    private SearchServiceClient searchServiceClient;
    @Override
    @Transactional
    public void add(CourseAddParamDTO dto) {
        Course course = dto.getCourse();
        CourseDetail courseDetail = dto.getCourseDetail();
        CourseMarket courseMarket = dto.getCourseMarket();
        //条件判断
        String name = course.getName();
        //条件对象
        Wrapper<Course> wrapper = new EntityWrapper<>();
        wrapper.eq("name",name);
        Course courseFromDB = selectOne(wrapper);
        if (courseFromDB!=null) throw new BusinessException("课程名重复");
        //保存课程，拿到id
        course.setStatus(CourseConstants.KEY_COURSE_STATUS_OFFLINE);
        course.setTenantId(26L);
        course.setTenantName("源码时代");
        course.setUserId(1L);
        course.setUserName("yhptest1");
        // TODO: 2022/4/11
        insert(course);
        //保存课程营销
        courseMarket.setId(course.getId());
        courseMarketService.insert(courseMarket);
        //保存课程详情
        courseDetail.setId(course.getId());
        courseDetailService.insert(courseDetail);
    }
    //课程上架
    @Override
    public void onLineCourse(Long courseId) {
        //* 1.参数判断
        Assert.assertNotNull(courseId, "非法请求");
        //* 2.条件判断：如果是未上架才能上架
        Course course = selectById(courseId);
        Assert.assertNotNull(course , "非法请求");
        if(course.getStatus().intValue() != CourseConstants.KEY_COURSE_STATUS_OFFLINE){
            throw new BusinessException("课程不处于待上线状态");
        }
        //* 3.修改数据库课程状态为上架
        course.setStatus(CourseConstants.KEY_COURSE_STATUS_ONLINE);
        updateById(course);

        //* 4.把课程数据存储到ES

        CourseMarket courseMarket = courseMarketService.selectById(courseId);

        CourseDoc doc = new CourseDoc();
        BeanUtils.copyProperties(course ,doc);
        BeanUtils.copyProperties(courseMarket ,doc);
        doc.setChargeName(courseMarket.getCharge() == 1?"免费":"收费");
        doc.setOnlineTime(new Date());
        AjaxResult ajaxResult = searchServiceClient.add(doc);
        Assert.assertTrue(ajaxResult.isSuccess(),"课程上架失败");
    }

}

